<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bagi_kelas extends CI_Controller {

	function __construct() {
        parent::__construct();
        $this->isLogin();	
		$this->load->model("master/master_th_ajaran"); 	
		$this->load->model("master/master_pegawai"); 	
		$this->load->model("master/master_kelas"); 	
		$this->load->model("master/master_lembaga"); 	
		$this->load->model("atur/pengaturan_kelas"); 			
		$this->load->model("bagi/pembagian_kelas"); 
		$this->num_post=10;					
	}
	
	function isLogin(){
		$login=$this->session->userdata('isLogin');
		if(! isset($login)||$login != true){
			$this->session->set_flashdata('error_login', 'Untuk Mengakses Admin Anda Harus Login!');
			redirect('login');	
		}
	}
	
	function index($offset = 0)
	{
		$data['main_view']='bagi/kelas/read';
		$uri_segment = 3;
		
		if($offset != 0){
			$offset = $this->uri->segment($uri_segment);
		}else{
			$offset=$offset;
		}
		$data['list'] = $this->master_th_ajaran->read($this->num_post, $offset);
		$data['offset']=$offset;
		$num_rows = $this->master_th_ajaran->num_rows();
			
		if($num_rows>0){
			// Membuat pagination
			$config['base_url'] = base_url().'bagi_kelas/index';
			$config['cur_tag_open'] = '<li class=active><a href="#">';
			$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			$config['prev_link'] = '<';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			$config['next_link'] = '>';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			//buat last
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			$config['last_link'] = '>>';
			//buat first
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			$config['first_link'] = '<<';
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $this->num_post;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
		}
		$this->load->view('index',$data);
		
	}	
	
	function lembaga($id_mr_th_ajaran)
	{
		$data['id_mr_th_ajaran']=$id_mr_th_ajaran;
		$data['list']=$this->master_lembaga->read();	
		$data['main_view']='bagi/kelas/lembaga';
		$this->load->view('index',$data);
	}

	function detail()
	{
		$id_mr_th_ajaran = $this->security->xss_clean($this->uri->segment(3));	
		$id_lembaga = $this->security->xss_clean($this->uri->segment(4));	
		$data['list']=$this->pembagian_kelas->detail($id_mr_th_ajaran,$id_lembaga);		
		$data['list_th']=$this->master_th_ajaran->detail($id_mr_th_ajaran);			
		$data['list_kelas']=$this->pengaturan_kelas->kelas_th_ajar($id_mr_th_ajaran,$id_lembaga);			
		$data['list_pegawai']=$this->master_pegawai->read();			
		$data['list_lembaga']=$this->master_lembaga->edit($id_lembaga);			
		$data['id_lembaga']=$id_lembaga;	
		$data['id_mr_th_ajaran']=$id_mr_th_ajaran;	
		$data['main_view']='bagi/kelas/detail';
		$this->load->view('index',$data);
	}
	
	function kelas()
	{
		$id_atur_kelas = $this->security->xss_clean($this->uri->segment(3));	
		$id_lembaga = $this->security->xss_clean($this->uri->segment(4));	
		$id_mr_th_ajaran = $this->security->xss_clean($this->uri->segment(5));	
		$data['list']=$this->pembagian_kelas->siswa($id_atur_kelas);		
		$data['list_meta']=$this->pembagian_kelas->meta($id_atur_kelas);		
		$data['list_siswa_baru']=$this->pembagian_kelas->siswa_baru($id_lembaga);		
		$data['list_kelas']=$this->pembagian_kelas->kelas_lembaga($id_lembaga);		
		$data['list_tajar']=$this->pembagian_kelas->th_lembaga($id_lembaga);		
		$data['list_kelas_pindah']=$this->pembagian_kelas->kelas_pindah($id_lembaga,$id_atur_kelas,$id_mr_th_ajaran);		
		$data['list_lembaga']=$this->master_lembaga->edit($id_lembaga);	
		$data['main_view']='bagi/kelas/kelas';
		$this->load->view('index',$data);
	}
	
	function add_exe()
	{	
		$list_siswa=$this->input->post('nis');
		$id_atur_kelas=$this->input->post('id_atur_kelas');

		if(isset($list_siswa)){
			foreach($list_siswa as $val){
				$dataInsert = array(
					'id_atur_kelas'=>$id_atur_kelas,
					'nis'=>$val
				);
			$this->pembagian_kelas->add($dataInsert);
			}
		}	
		$back = htmlspecialchars($_SERVER['HTTP_REFERER']);
		redirect ($back);
	}
	
	function list_siswa()
	{
		$id_mr_kelas=$this->input->post('id_mr_kelas');
		$id_mr_th_ajaran=$this->input->post('id_mr_th_ajaran');
		$id_atur_kelas=$this->input->post('id_atur_kelas');
		$id_lembaga=$this->input->post('id_lembaga');
		
		$data['list_siswa']=$this->pembagian_kelas->list_siswa($id_mr_kelas,$id_mr_th_ajaran,$id_atur_kelas,$id_lembaga);	
		$this->load->view('bagi/kelas/list_siswa',$data);
	}

	function list_siswa_pindah()
	{
		$id_mr_kelas=$this->input->post('id_mr_kelas');
		$id_mr_th_ajaran=$this->input->post('id_mr_th_ajaran');
		$id_atur_kelas=$this->input->post('id_atur_kelas');
		$id_lembaga=$this->input->post('id_lembaga');
		
		$data['list_siswa']=$this->pembagian_kelas->list_siswa($id_mr_kelas,$id_mr_th_ajaran,$id_atur_kelas,$id_lembaga);	
		$this->load->view('bagi/kelas/list_siswa_pindah',$data);
	}

	function pindah()
	{	
		$list_id_bagi_kelas=$this->input->post('id_bagi_kelas');
		$id_atur_kelas=$this->input->post('id_atur_kelas');

		if(isset($list_id_bagi_kelas)){
			foreach($list_id_bagi_kelas as $id_bagi_kelas){
				$id_bagi_kelas = $id_bagi_kelas;
				$dataUpdate = array(
					'id_atur_kelas'=>$id_atur_kelas					
				);
			$this->pembagian_kelas->pindah($dataUpdate,$id_bagi_kelas);
			}
		}
		$back = htmlspecialchars($_SERVER['HTTP_REFERER']);
		redirect ($back);
	}
	
	function delete()
	{
		$id_bagi_kelas = $this->security->xss_clean($this->uri->segment(3));
		$this->pembagian_kelas->delete($id_bagi_kelas);
		$back = htmlspecialchars($_SERVER['HTTP_REFERER']);
		redirect ($back);
	}	

}